package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account")
@SuppressWarnings({"all"})
public class UserAccountApiController {

	@Autowired
	private UserAccountService userAccountService;

	/**
	 * 获取当前用户账户可用余额
	 * @return
	 */
	@GuiGuLogin
	@Operation(summary = "获取当前用户账户可用余额")
	@GetMapping("/userAccount/getAvailableAmount")
	public Result<BigDecimal> getAvailableAmount(){
		BigDecimal availableAmount = userAccountService.getAvailableAmount();
		return Result.ok(availableAmount);
	}

	/**
	 * 检查余额及扣减余额
	 * TODO:根虚拟物品发货接口保持一致，用户ID通过入参对象传递。
	 *		 将来采用微信/支付宝支付后，支付宝会有支付成功回调 -> 请求商户系统不会携带Token访问
	 * @param accountDeductVo 订单编号、扣减金额、购买内容、用户Id
	 * @return
	 */
	@Operation(summary = "检查余额及扣减余额")
	@PostMapping("/userAccount/checkAndDeduct")
	public Result checkAndDeduct(@RequestBody AccountDeductVo accountDeductVo){
		userAccountService.checkAndDeduct(accountDeductVo);
		return Result.ok();
	}
}

